﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Styx.WoWInternals.DBC;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Common;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.Pathing;
using Styx;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Styx.CommonBot;
using System.Windows.Forms;

namespace KingWoW
{
    class KingWoW : CombatRoutine
    {
        public override sealed string Name { get { return "KingWoW 2 Paladin"; } }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        //private SvnManager svn = new SvnManager();
        
        public override WoWClass Class
        {
            get
            {
                if (Me.Class == WoWClass.Paladin &&
                    (StyxWoW.Me.Specialization == WoWSpec.PaladinProtection || StyxWoW.Me.Specialization == WoWSpec.PaladinRetribution))
                    return WoWClass.Paladin;
                return WoWClass.None;
            }
        }
        private WoWSpec CurrentSpec = WoWSpec.None;

        private KingWoWUtility utils = new KingWoWUtility();
  
        private static KingWoWAbstractBaseClass KingWoWBaseClass;
        public Styx.Helpers.Settings settingsBase;
        public string activePath;
        private KingWoWUtility u = new KingWoWUtility();
        private KingHealSettingsGUIFORM Settings_Form = null;

        public KingWoWAbstractBaseClass ActiveKingWoWClass
        {
            get
            {
                // SPEC
                CurrentSpec = StyxWoW.Me.Specialization;
                
                switch (CurrentSpec)
                {
                    
                    case WoWSpec.PaladinRetribution:
                        {
                            if (KingWoWBaseClass == null)
                                KingWoWBaseClass = new RetriPaladinCombatClass();
                            break;
                        }
                    case WoWSpec.PaladinProtection:
                        {
                            if (KingWoWBaseClass == null)
                                KingWoWBaseClass = new ProtPaladinCombatClass();
                            break;
                        }
                   
                    
                    default:
                        {
                            KingWoWBaseClass = null;
                            break;
                        }
                }
                if (KingWoWBaseClass == null)
                {
                    utils.LogActivity("Current spec: "+CurrentSpec.ToString()+" not Supported Spec atm stay tuned for update");
                }
                
                return KingWoWBaseClass;
            }
        }

        public Styx.Helpers.Settings ActiveSettings
        {
            get
            {
                switch (CurrentSpec)
                {
                    
                    case WoWSpec.PaladinRetribution:
                        {
                            if (settingsBase == null)
                                settingsBase = RetriPaladinSettings.Instance;
                            break;
                        }
                    case WoWSpec.PaladinProtection:
                        {
                            if (settingsBase == null)
                                settingsBase = ProtPaladinSettings.Instance;
                            break;
                        }
                    
                    default:
                        {
                            settingsBase = null;
                            break;
                        }
                }
                if (settingsBase == null)
                {
                    utils.LogActivity("No Setting for this spec please stay tuned for updates");
                    TreeRoot.Stop();
                }

                return settingsBase;   
            }
        }

        public string ActivePath
        {
            get
            {
                switch (CurrentSpec)
                {
                    
                    case WoWSpec.PaladinRetribution:
                        {
                            if (activePath == null)
                                activePath = RetriPaladinSettings.Instance.path_name;
                            break;
                        }
                    case WoWSpec.PaladinProtection:
                        {
                            if (activePath == null)
                                activePath = ProtPaladinSettings.Instance.path_name;
                            break;
                        }
                    
                    default:
                        {
                            activePath = null;
                            break;
                        }
                }
                if (activePath == null)
                {
                    utils.LogActivity("No Setting path for this spec please stay tuned for updates");
                    TreeRoot.Stop();
                }

                return activePath;
            }
        }


        public override void Combat()
        {
            if (ActiveKingWoWClass.Combat) return;
        }

        public override bool WantButton
        {
            get
            {
                return true;
            }
        }

        public override void OnButtonPress()
        {
            if (Settings_Form == null && ActiveSettings != null && ActivePath != null && ExtraUtilsSettings.Instance.path_name != null)
            {
                Settings_Form = new KingHealSettingsGUIFORM(ActiveSettings, ActivePath, ExtraUtilsSettings.Instance.path_name);
            }

            if (Settings_Form != null)
            {
                //Settings_Form.ShowDialog();
                try
                {
                    Settings_Form.ShowDialog();
                }
                catch (Exception)
                {
                    Logging.Write("Exception Thrown (Calling Settings_Form): ");
                }
            }

        }

        public override void Pulse()
        {
            if (ActiveKingWoWClass.Pulse) return;
        }

        public override void Initialize()
        {
            Lua.Events.AttachEvent("PLAYER_SPECIALIZATION_CHANGED", UpdateSpecializzation);
            Lua.Events.AttachEvent("ACTIVE_TALENT_GROUP_CHANGED", UpdateTalents);
            BotEvents.OnBotStartRequested += new BotEvents.OnBotStartStopRequestedDelegate(BotEvents_OnBotStart);
            if (ActiveKingWoWClass.Initialize) return;
        }

        private void UpdateTalents(object sender, LuaEventArgs args)
        {
            Logging.Write("************************");
            Logging.Write("************************");
            Logging.Write("Update Talents and Glyph");
            Logging.Write("************************");
            Logging.Write("************************");
            if (ActiveKingWoWClass.UpdateTalentAndGlyph) return;
        }

        void BotEvents_OnBotStart(EventArgs args)
        {
            //if (StyxWoW.Me.Specialization != CurrentSpec)
            //{

                if (Settings_Form != null)
                {
                    Settings_Form.Close();
                    Settings_Form.Dispose();
                    Settings_Form = null;
                }
                KingWoWBaseClass = null;
                settingsBase = null;
                Settings_Form = null;
                activePath = null;
                Reinitialize();

                if (ActiveKingWoWClass.UpdateTalentAndGlyph) return;
            //}

        }

        private void Reinitialize()
        {
            Logging.Write("************************");
            Logging.Write("************************");
            Logging.Write("Reinitializating CR");
            Logging.Write("************************");
            Logging.Write("************************");
            if (ActiveKingWoWClass.Initialize) return;
        }

        private void UpdateSpecializzation(object sender, LuaEventArgs args)
        {
            Logging.Write("************************");
            Logging.Write("************************");
            Logging.Write("UpdateSpecializzation");
            Logging.Write("************************");
            Logging.Write("************************");

            if (Settings_Form != null)
            {
                Settings_Form.Close();
                Settings_Form.Dispose();
                Settings_Form = null;
            }
            KingWoWBaseClass = null;
            settingsBase = null;
            Settings_Form = null;
            activePath = null;
            Reinitialize();

            if (ActiveKingWoWClass.UpdateTalentAndGlyph) return;
        }

        public override bool NeedRest
        {
            get
            {
                return ActiveKingWoWClass.NeedRest;
            }
        }

        public override bool NeedCombatBuffs
        {
            get
            {
                return ActiveKingWoWClass.NeedCombatBuffs;
            }
        }

        public override bool NeedPreCombatBuffs
        {
            get
            {
                return ActiveKingWoWClass.NeedPreCombatBuffs;
            }
        }

        public override bool NeedPullBuffs
        {
            get
            {
                return ActiveKingWoWClass.NeedPullBuffs;
            }
        }

        public override void Pull()
        {
            if(ActiveKingWoWClass.Pull) return;
        }

    }
}
